﻿using Microsoft.Extensions.Logging;
using ProductTool.Entity;
using ProductTool.Tools;
using SmartSql;
using SmartSql.Abstractions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace ProductTool.Dao
{
    class ItemDao
    {
        ISmartSqlMapper sqlMapper = null;
        public ItemDao()
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                LoggerFactory = loggerFactory,
                ConfigPath = "SmartSqlMapSqlServer.xml"
            });
        }

        public List<JDItem> QueryJdItem(int pageIndex,int pageSize,string goods_sn="")
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                LoggerFactory = loggerFactory,
                ConfigPath = "SmartSqlMapSqlServer.xml"
            });
            var result = sqlMapper.Query<JDItem>(new RequestContext
            {
                Scope = "Item",
                SqlId = "QueryJdItem",
                Request = new
                {
                    startIndex= PageHelper.getPageIndex(pageIndex, pageSize).Item1,
                    endIndex = PageHelper.getPageIndex(pageIndex, pageSize).Item2,
                    goods_sn
                }
            });
            return result.ToList();
        } 
        public List<Property> QueryCategoryProp()
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                LoggerFactory = loggerFactory,
                ConfigPath = "SmartSqlMapMysql.xml"
            });
            var result = sqlMapper.Query<Property>(new RequestContext
            {
                Scope = "Item",
                SqlId = "QueryCategoryProp",
                Request = new
                {
                 
                   
                }
            });
            return result.ToList();
        } 
        public List<PropertyValue> QueryPropValue()
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                LoggerFactory = loggerFactory,
                ConfigPath = "SmartSqlMapMysql.xml"
            });
            var result = sqlMapper.Query<PropertyValue>(new RequestContext
            {
                Scope = "Item",
                SqlId = "QueryPropValue",
                Request = new {                
                
                }
            });
            return result.ToList();
        }


        public int CreatePropValue(PropertyValue pv)
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                LoggerFactory = loggerFactory,
                ConfigPath = "SmartSqlMapMysql.xml"
            });
            var result = sqlMapper.ExecuteScalar<int>(new RequestContext
            {
                Scope = "Item",
                SqlId = "CreatePropValue",
                Request = pv
            });
            return result;
        }

        public void CreateItemPropValue(List<ItemPropValue> pv)
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                LoggerFactory = loggerFactory,
                ConfigPath = "SmartSqlMapMysql.xml"
            });
            var result = sqlMapper.Query<PropertyValue>(new RequestContext
            {
                Scope = "Item",
                SqlId = "CreateItemPropValue",
                Request = new
                {
                    dataList = pv
                }
            });
        }
         public void UpdateItemPropDetail(string item_number,string detail)
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                LoggerFactory = loggerFactory,
                ConfigPath = "SmartSqlMapMysql.xml"
            });
            var result = sqlMapper.Query<PropertyValue>(new RequestContext
            {
                Scope = "Item",
                SqlId = "UpdateItemPropDetail",
                Request = new {

                    item_number,
                    detail_specification= detail
                }
            });
        }

        public void UpdateJDItemPropDetail(string sku)
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                LoggerFactory = loggerFactory,
                ConfigPath = "SmartSqlMapSqlServer.xml"
            });
            var result = sqlMapper.Query<PropertyValue>(new RequestContext
            {
                Scope = "Item",
                SqlId = "UpdateJDItemPropDetail",
                Request = new
                {
                   sku
                }
            });
        }

        public List<ItemEntity> CheckJDInIM(List<string> goods_snList)
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                LoggerFactory = loggerFactory,
                ConfigPath = "SmartSqlMapMysql.xml"
            });
            var result = sqlMapper.Query<ItemEntity>(new RequestContext
            {
                Scope = "Item",
                SqlId = "CheckJDInIM",
                Request = new
                {
                    goods_snList
                }
            });
            return result.ToList();
        }
        public List<ItemEntity> QueryAllItem(int pageIndex,int pageSize)
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                LoggerFactory = loggerFactory,
                ConfigPath = "SmartSqlMapMysql.xml"
            });
            var result = sqlMapper.Query<ItemEntity>(new RequestContext
            {
                Scope = "Item",
                SqlId = "QueryAllItem",
                Request = new
                {
                    pageIndex= ( pageIndex - 1) * pageSize ,
                    pageSize 
                }
            });
            return result.ToList();
        }
        public string QueryMaxGoodsSN()
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                LoggerFactory = loggerFactory,
                ConfigPath = "SmartSqlMapMysql.xml"
            });
            var result = sqlMapper.ExecuteScalar<string>(new RequestContext
            {
                Scope = "Item",
                SqlId = "QueryMaxGoodsSN",
                Request = new
                {

                }
            });
            return result;
        }

        public string QueryMaxGoodsSNByProp()
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                LoggerFactory = loggerFactory,
                ConfigPath = "SmartSqlMapMysql.xml"
            });
            var result = sqlMapper.ExecuteScalar<string>(new RequestContext
            {
                Scope = "Item",
                SqlId = "QueryMaxGoodsSNByProp",
                Request = new
                {

                }
            });
            return result;
        }
        public List<Property> QueryProperty(int category_id)
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {             
                ConfigPath = "SmartSqlMapMysql.xml"
            });
            var result = sqlMapper.Query<Property>(new RequestContext
            {
                Scope = "Item",
                SqlId = "QueryProperty",
                Request = new
                {
                    category_id
                }
            });
            return result.ToList();
        }

        public DbTable QueryNoImageForBook(int top)
        {
            var result = sqlMapper.GetDbTable(new RequestContext
            {
                Scope = "Item",
                SqlId = "QueryNoImageForBook",
                Request = new
                {
                    top
                }                 
            });
            return result;
        }
        public void UpdateBookImage(string image_name,string sku)
        {        
            var result = sqlMapper.Query<Property>(new RequestContext
            {
                Scope = "Item",
                SqlId = "UpdateBookImage",
                Request = new
                {
                    image_name,
                    sku
                }
            });           
        }

        public List<ItemEntity> QueryItemList(int pageIndex, int pageSize)
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                LoggerFactory = loggerFactory,
                ConfigPath = "SmartSqlMapMysql.xml"
            });
            var result = sqlMapper.Query<ItemEntity>(new RequestContext
            {
                Scope = "Item",
                SqlId = "QueryItemList",
                Request = new
                {
                    pageIndex = (pageIndex - 1) * pageSize,
                    pageSize
                }
            });
            return result.ToList();
        }
        public ItemEntity CheckItemExists(string goods_sn)
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                LoggerFactory = loggerFactory,
                ConfigPath = "SmartSqlMapMysql.xml"
            });
            var result = sqlMapper.QuerySingle<ItemEntity>(new RequestContext
            {
                Scope = "Item",
                SqlId = "CheckItemExists",
                Request = new
                {
                    goods_sn
                }
            });
            return result;
        }
        public JDItem QueryJdItemBySku(string sku)
        {
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {               
                ConfigPath = "SmartSqlMapSqlServer.xml"
            });
            var result = sqlMapper.QuerySingle<JDItem>(new RequestContext
            {
                Scope = "Item",
                SqlId = "QueryJdItemBySku",
                Request = new
                {
                    sku
                }
            });
            return result;
        }
        public int QuerySingleCategory(string name)
        {
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                ConfigPath = "SmartSqlMapMysql.xml"
            });
            var result = sqlMapper.QuerySingle<int>(new RequestContext
            {
                Scope = "Item",
                SqlId = "QuerySingleCategory",
                Request = new
                {
                    category_name = name
                }
            });
            return result;
        }

        public List<JDItem> GetEmptyImgItem()
        {
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                ConfigPath = "SmartSqlMapSqlServer.xml"
            });
            var result = sqlMapper.Query<JDItem>(new RequestContext
            {
                Scope = "Item",
                SqlId = "GetEmptyImgItem",
                Request = new
                {
                  
                }
            });
            return result.ToList();
        }
         public int UpdateImageFlag(string sku)
        {
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                ConfigPath = "SmartSqlMapSqlServer.xml"
            });
            var result = sqlMapper.Execute(new RequestContext
            {
                Scope = "Item",
                SqlId = "UpdateImageFlag",
                Request = new
                {
                   sku
                }
            });
            return result;
        }
        public int CreateItemImage(string sku,string image_name)
        {
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                ConfigPath = "SmartSqlMapMysql.xml"
            });
            var result = sqlMapper.Execute(new RequestContext
            {
                Scope = "Item",
                SqlId = "CreateItemImage",
                Request = new
                {
                    sku,
                    image_name
                }
            });
            return result;
        } 
        public List<string> getJDItemNoInv()
        {
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                ConfigPath = "SmartSqlMapSqlServer.xml"
            });
            var result = sqlMapper.Query<string>(new RequestContext
            {
                Scope = "Item",
                SqlId = "getJDItemNoInv",
                Request = new
                {
                   
                }
            });
            return result.ToList();
        }
        public int CreateJDItemInv(string sku, string stock)
        {
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                ConfigPath = "SmartSqlMapSqlServer.xml"
            });
            var result = sqlMapper.Execute(new RequestContext
            {
                Scope = "Item",
                SqlId = "CreateJDItemInv",
                Request = new
                {
                    sku,
                    stock
                }
            });
            return result;
        }
    }
}
